/** 新建筑 */
cc.Class({
    extends: cc.Component,

    properties: {
        /** 新建筑图片 */
        sp_img:[cc.Sprite],
        /** 新建按钮 */
        bt_new:[cc.Button],
        /** 建筑名字 */
        txt_name:[cc.Label],
        /** 建筑说明 */
        txt_demo:[cc.Label],
        /** 当前位置id */
        _aid:cc.Integer,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        for(let i=0;i<this.bt_new.length;i++){
            this.bt_new[i].node.on(cc.Node.EventType.TOUCH_END, function(ev){
                let sd = [2,13,{aid:this._aid,bid:0,cid:App.cid}];
                if(ev.target.name == "btnew0"){
                    if(this._aid < 12){
                        sd[2].bid = this._aid;
                    }else{
                        sd[2].bid = 21;
                    }
                }else if(ev.target.name == "btnew1"){
                    sd[2].bid = 22;
                }else if(ev.target.name == "btnew2"){
                    sd[2].bid = 23;
                }else if(ev.target.name == "btnew3"){
                    sd[2].bid = 24;
                }
                let cb = true, bd = App.C.build[sd[2].bid].list[0], ct = App.U.city[App.cid], tip = "名称：<b><color=#C5C21F>" + App.C.build[sd[2].bid].name + "</color></b>";
                if(bd[0].length > 0){
                    tip += "<br/>前提："
                    for(let i=0;i<bd[0].length;i+=2){
                        if(bd[0][i] < 12){
                            if(ct.area[bd[0][i]-1] && ct.area[bd[0][i]-1].lev >= bd[0][i+1]){
                                tip += " " + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name;
                            }else{
                                tip += " <color=#FF0000>" + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name + "</color>";
                                cb = false;
                            }
                        }else{
                            let fd = false;
                            for(let j=11;j<ct.area.length;j++){
                                if(ct.area[j].bid == bd[0][i] && ct.area[j].lev >= bd[0][i+1]){
                                    fd = true;
                                    break;
                                }
                            }
                            if(fd){
                                tip += " " + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name;
                            }else{
                                tip += " <color=#FF0000>" + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name + "</color>";
                                cb = false;
                            }
                        }
                    }
                }
                let re = ["coin","wood","ore","sulfur","mercury","crystal","stone"], ren = ["金币","木头","矿石","硫磺","水银","水晶","宝石"];
                tip += "<br/><br/>[<color=#dcc86a>升级所需资源</color>]";
                for(let n=0;n<re.length;n++){
                    if(bd[5+n] > App.U.res[re[n]]){
                        tip += "<br/>"+ren[n]+":<color=#FF0000>"+bd[5+n]+"</color>";
                        cb = false;
                    }else{
                        tip += "<br/>"+ren[n]+":"+bd[5+n];
                    }
                }
                tip += "<br/><br/>耗时：" + bd[4] + "秒";
                //cb = false;
                //cc.log(tip);
                if(cb == false){
                    App.showtip(tip);
                    return;
                } 
                App.net.send(sd);
            }, this);
        }
    },

    start () {

    },

    /** 设置位置id */
    setAid(aid){
        this._aid = aid;

        let race = "r"+App.U.city[App.cid].race+"_";
        let bs = cc.find("WinBase", this.node).getComponent("winbase");
        if(aid < 12){
            this.sp_img[0].spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + aid + ".png");
            bs.setInfo(520, 130, App.C.build[aid].name);
            this.txt_name[0].string = App.C.build[aid].name;
            this.txt_demo[0].string = App.C.build[aid].desc.replace("{0}", "1级").replace("{2}", App.C.build[aid].list[0][2]);
        }else{
            this.sp_img[0].spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + "15.png");
            this.txt_name[0].string = App.C.build[21].name;
            this.txt_demo[0].string = App.C.build[21].desc.replace("{0}", "1级").replace("{2}", App.C.build[21].list[0][2]);
            this.sp_img[1].spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + "12.png");
            this.txt_name[1].string = App.C.build[22].name;
            this.txt_demo[1].string = App.C.build[22].desc.replace("{0}", "1级").replace("{2}", App.C.build[22].list[0][2]);
            this.sp_img[2].spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + "13.png");
            this.txt_name[2].string = App.C.build[23].name;
            this.txt_demo[2].string = App.C.build[23].desc.replace("{0}", "1级").replace("{2}", App.C.build[23].list[0][2]);
            this.sp_img[3].spriteFrame = new cc.SpriteFrame(App.path + "race/" + race + "14.png");
            this.txt_name[3].string = App.C.build[24].name;
            this.txt_demo[3].string = App.C.build[24].desc.replace("{0}", "1级").replace("{2}", App.C.build[24].list[0][2]);
            bs.setInfo(520,430,"选择自由建筑类型");
        }
    },

    // update (dt) {},
});
